package com.kaiwukj.android.ufamily.mvp.ui.page.mine.house.door;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerMineComponent;
import com.kaiwukj.android.ufamily.di.module.MineModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessDeviceResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessRecordResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseMemberResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.house.MyHousePresenter;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.DoorRecordUrl)
public class DoorRecordActivity extends BaseMvpActivity<MyHousePresenter> implements MineContract.HouseView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private DoorRecordAdapter mRecordAdapter;

    private int page = 1;
    private int pageSize = 12;
    private boolean isLoadingMore = false;

    private String monthStr = "2020-06";

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerMineComponent.builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_door_record;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("开门记录");
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        monthStr = String.format("%s-%s", year, month + 1);
        tvDate.setText(String.format("%s年%s月", year, month + 1));
        tvDate.setOnClickListener(v -> showDatePicker());
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadingMore = true;
                ++page;
                getRecord();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getRecord();
            }
        });
        mRecordAdapter = new DoorRecordAdapter();
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(1));
        rvList.setAdapter(mRecordAdapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        emptyView.show(true);
        getRecord();
    }

    private void getRecord() {
        mPresenter.fetchDeviceEventRecords(monthStr, page, pageSize);
    }

    @Override
    public void onFetchHouse(List<MyHouseResult> results) {

    }

    @Override
    public void onFetchHouseMember(List<MyHouseMemberResult> results) {

    }

    @Override
    public void onFetchDoorAccess(DoorAccessDeviceResult result) {

    }

    @Override
    public void onFetchDoorRecord(List<DoorAccessRecordResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            mRecordAdapter.addData(results);
            if (pageSize > results.size()) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            refreshLayout.finishRefresh(true);
            mRecordAdapter.setList(results);
        }
        if (mRecordAdapter.getItemCount() == 0) {
            emptyView.show("", "没有查询到记录!");
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
        emptyView.show("", "查询数据异常!!!");
    }

    private void showDatePicker() {
        new TimePickerBuilder(this, (date, v) -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            tvDate.setText(String.format("%s年%s月", year, month + 1));
            monthStr = String.format("%s-%s", year, month + 1);

            refreshLayout.autoRefresh();

            //ToastUtils.showShort("选择的月份是:" + month + 1);
        }).setType(new boolean[]{true, true, false, false, false, false})
                .setCancelColor(ContextCompat.getColor(this, R.color.common_text_slight_color))
                .setSubmitColor(ContextCompat.getColor(this, R.color.app_color_theme))
                .setOutSideCancelable(true)
                .isCyclic(true).build().show();
    }
}
